﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using Module;
using EMPower.Utility;
using BLL.Common;
using BLL;

public partial class User_UserAdd : System.Web.UI.Page
{
    public string City = "城市";
    public bool isReadOnly = true;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // 获取地区
            BindProvince();
            BindUserType();

            this.Btn_OK.Visible = true;
            this.UpdateButton.Visible = false;

            string userProIdStr = Request.QueryString["UID"];
            if (!string.IsNullOrWhiteSpace(userProIdStr))
            {
                string decryUserProIdStr = "";// Contexts.GetDecrypt(userProIdStr);
                try
                {
                    decryUserProIdStr = Contexts.GetDecrypt(userProIdStr);
                }
                catch (Exception ex)
                {
                }
                if (string.IsNullOrWhiteSpace(decryUserProIdStr))
                {
                    decryUserProIdStr = userProIdStr;
                }
                long userProId = Convert.ToInt64(decryUserProIdStr);
                this.userPassPortId.Value = decryUserProIdStr;
                UserProfileInfo userInfo = BLL.BUserProfile.getInstance().GetUserProfileInfo(userProId);
                UserPassportInfo userPassportInfo = BLL.BUserPassport.getInstance().GetUserPassportInfo(userProId);
                if (userInfo != null)
                {
                    this.Btn_OK.Visible = false;
                    this.UpdateButton.Visible = true;
                    this.txt_userName.ReadOnly = true;
                    this.txt_email.ReadOnly = true;
                    this.txt_PhoneNumber.ReadOnly = true;

                    // 设置各个字段值
                    this.DDL_userType.SelectedValue = CommonUtils.GetRoleValue(Convert.ToString(userInfo.UserType));
                    this.txt_userName.Text = userPassportInfo.UserName;
                    this.txt_password.Attributes.Add("value", userPassportInfo.Password);
                    //this.txt_password.Text = userPassportInfo.Password;
                    //this.txt_password.TextMode = TextBoxMode.Password;
                    //this.txt_repPassword.Text = userPassportInfo.Password;
                    this.txt_repPassword.Attributes.Add("value", userPassportInfo.Password);
                    //this.txt_repPassword.TextMode = TextBoxMode.Password;
                    this.txt_reanName.Text = userInfo.RealName;
                    this.txt_nickName.Text = userInfo.NickName;
                    this.txt_IdNum.Text = userInfo.IDCardNum;
                    this.txt_email.Text = userInfo.Email;
                    this.txt_PhoneNumber.Text = userInfo.Mobile;
                    if (userInfo.Gender == 0)
                    {
                        this.rb_man.Checked = true;
                    }
                    else
                    {
                        this.rb_woman.Checked = true;
                    }
                    this.txt_birthday.Text = userInfo.Birthday.ToString("yyyy-MM-dd");
                    this.DDL_Education.SelectedValue = Convert.ToString(userInfo.Education);
                    this.DDL_province.SelectedValue = Convert.ToString(userInfo.ProvinceId);
                    if (!"999".Equals(this.DDL_province.SelectedValue))
                    {
                        bindCity();
                        this.DDL_city.SelectedValue = Convert.ToString(userInfo.CityId);
                    }
                    this.txt_address.Text = userInfo.Adress;
                    this.DDL_PersonalIncome.SelectedValue = Convert.ToString(userInfo.PersonalInCome);
                    this.DDL_FamilyIncome.SelectedValue = Convert.ToString(userInfo.FamilyInCome);
                    this.updateflg.Value = "1";
                }
            }
        }
    }

    private void BindUserType()
    {
        ListItem item = new ListItem();
        item.Text = "请选择";
        item.Value = "-1";
        ListItem item1 = new ListItem();
        item1.Text = "自强会员";
        item1.Value = "1";
        ListItem item2 = new ListItem();
        item2.Text = "天使会员";
        item2.Value = "2";
        ListItem item3 = new ListItem();
        item3.Text = "后台管理员";
        item3.Value = "3";
        item3.Selected = true;
        ListItem item4 = new ListItem();
        item4.Text = "公益团体";
        item4.Value = "4";
        this.DDL_userType.Items.Add(item);
        this.DDL_userType.Items.Add(item1);
        this.DDL_userType.Items.Add(item2);
        this.DDL_userType.Items.Add(item3);
        this.DDL_userType.Items.Add(item4);

    }

    void BindProvince()
    {
        
            this.DDL_province.Items.Clear();
            List<AreaInfo> areas = BArea.getInstance().GetAreaByParentId(0);
            if (areas != null && areas.Count > 0)
            {
                this.DDL_province.DataSource = areas;
                this.DDL_province.DataTextField = "AreaName";
                this.DDL_province.DataValueField = "AreaCode";
                this.DDL_province.DataBind();
            } 
        
    }

    void bindCity()
    {
        this.DDL_city.Items.Clear();
        int provinceid = 0;
        if (int.TryParse(this.DDL_province.SelectedValue, out provinceid))
        {

            List<AreaInfo> areas = BArea.getInstance().GetAreaByParentId(provinceid);
            if (areas != null && areas.Count > 0)
            {
                this.DDL_city.DataSource = areas;
                this.DDL_city.DataTextField = "AreaName";
                this.DDL_city.DataValueField = "AreaCode";
                this.DDL_city.DataBind();
            }
        }
    }

    protected void DDL_province_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindCity();
    }

    protected void Btn_OK_Click(object sender, EventArgs e)
    {
        // 保存内容
        UserPassportInfo userInfo = getUserProfile();

        if (userInfo == null || userInfo.UserPassportId == 0)
        {
            return;
        }
        BLL.BUserPassport.getInstance().Insert(userInfo);
        BLL.BUserProfile.getInstance().Insert(userInfo.ProfileInfo);
        Response.Redirect("UserList.aspx");
    }

    private UserPassportInfo getUserProfile()
    {
        UserPassportInfo loginInfo = new UserPassportInfo();
        UserProfileInfo current = new UserProfileInfo();
        loginInfo.UserPassportId = IdHelper.GetLongId();
        loginInfo.UserName = this.txt_userName.Text;
        loginInfo.Status = 1;
        loginInfo.Password = this.txt_password.Text;
        loginInfo.LoginCount = 0;
        loginInfo.LastLogin = DateTime.Now;
        loginInfo.DateCreated = DateTime.Now;

        current.UserPassportId = loginInfo.UserPassportId;
        current.DateCreated = DateTime.Now;
        current.Email = this.txt_email.Text;
        current.Mobile = txt_PhoneNumber.Text;
        current.UserType = Convert.ToInt16(this.DDL_userType.SelectedValue);
        current.IDCardNum = this.txt_IdNum.Text;
        current.UserImage = "";
        current.NickName = this.txt_nickName.Text;
        // 需要确定角色对应数字 TODO::::
        current.UserType = Convert.ToInt16(this.DDL_userType.SelectedValue);
        current.Gender = this.rb_man.Checked ? 1 : 2;
        current.IsFamous = 0;
        if (string.IsNullOrWhiteSpace(this.txt_birthday.Text))
        {
            current.Birthday = DateTime.Now.AddYears(-35);
        }
        else
        {
            current.Birthday = DateTime.Parse(this.txt_birthday.Text);
        }
        current.Education = Convert.ToInt16(this.DDL_Education.SelectedValue);
        try
        {
            if (string.Compare("999", this.DDL_province.SelectedValue) != 0)
            {
                current.ProvinceId = Convert.ToInt32(this.DDL_province.SelectedValue);
            }
            else
            {
                current.ProvinceId = -1;
            }
            if (string.Compare("99999", this.DDL_city.SelectedValue) != 0)
            {
                current.CityId = Convert.ToInt32(this.DDL_city.SelectedValue);
            }
            else
            {
                current.CityId = -1;
            }
        }
        catch (Exception ex)
        {
        }
        current.VillageId = -1;
        current.Adress = this.txt_address.Text;
        current.PersonalInCome = Convert.ToInt16(this.DDL_PersonalIncome.SelectedValue);
        current.FamilyInCome = Convert.ToInt16(this.DDL_FamilyIncome.SelectedValue);
        current.Credit = "";
        current.Dedication = "";
        current.LastUpdate = DateTime.Now;
        current.DateCreated = DateTime.Now;
        current.IDCardValidate = 1;
        current.RealName = this.txt_reanName.Text;

        //上传图片
        //int filelenth = file.PostedFile.ContentLength;
        //int fileSize = Convert.ToInt32((filelenth) / 1024);
        //if (fileSize > 5000)
        //{
        //    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('图片大小不超过5M');</script>");
        //    return null;
        //}
        //string fileType = file.FileName.Substring(file.FileName.LastIndexOf("."));
        //string strFileName = IdHelper.GetLongId().ToString() + "_" + DateTime.Now.Millisecond.ToString() + fileType;
        //string strNewFileName = BLL.Common.UploadResource.GetPath(BLL.Common.BizResourceType.UserResource, BLL.Common.BizResourceAccess.Upload) + strFileName;
        //file.SaveAs(strNewFileName);
        //current.IDCardImg = strFileName;

        current.IDCardImg = "";
        loginInfo.ProfileInfo = current;

        return loginInfo;
    }
    protected void UpdateButton_Click(object sender, EventArgs e)
    {
        string userPassportIdStr = this.userPassPortId.Value;
        long userPassportId = Convert.ToInt64(userPassportIdStr);
        UserPassportInfo userPassPortInfo = BLL.BUserPassport.getInstance().GetUserPassportInfo(userPassportId);
        UserProfileInfo userProfileInfo = BLL.BUserProfile.getInstance().GetUserProfileInfo(userPassportId);

        // 更新内容
        UpdateUserPassportInfo(userPassPortInfo);
        UpdateUserProfileInfo(userProfileInfo);

        if (userPassPortInfo == null || userPassPortInfo.UserPassportId == 0)
        {
            return;
        }
        BLL.BUserPassport.getInstance().Update(userPassPortInfo);
        BLL.BUserProfile.getInstance().Update(userProfileInfo);
        Response.Redirect("UserList.aspx");
    }

    private void UpdateUserPassportInfo(UserPassportInfo userPassPortInfo)
    {
        try
        {
            //loginInfo.UserPassportId = IdHelper.GetLongId();
            userPassPortInfo.UserName = this.txt_userName.Text;
            userPassPortInfo.Status = 1;
            userPassPortInfo.Password = this.txt_password.Text;
            //loginInfo.LoginCount = 0;
            //loginInfo.LastLogin = DateTime.Now;
            //loginInfo.DateCreated = DateTime.Now;
        }
        catch (Exception ex)
        {
        }
    }

    private void UpdateUserProfileInfo(UserProfileInfo userProfileInfo)
    {
        try
        {
            //current.UserPassportId = loginInfo.UserPassportId;
            //current.DateCreated = DateTime.Now;
            userProfileInfo.Email = this.txt_email.Text;
            userProfileInfo.Mobile = txt_PhoneNumber.Text;
            userProfileInfo.UserType = Convert.ToInt16(this.DDL_userType.SelectedValue);
            userProfileInfo.IDCardNum = this.txt_IdNum.Text;
            userProfileInfo.UserImage = "";
            userProfileInfo.NickName = this.txt_nickName.Text;
            // 需要确定角色对应数字 TODO::::
            //current.UserType = Convert.ToInt16(this.DDL_userType.SelectedValue);
            userProfileInfo.Gender = this.rb_man.Checked ? 1 : 2;
            userProfileInfo.IsFamous = 0;
            if (string.IsNullOrWhiteSpace(this.txt_birthday.Text))
            {
                userProfileInfo.Birthday = DateTime.Now.AddYears(-35);
            }
            else
            {
                userProfileInfo.Birthday = DateTime.Parse(this.txt_birthday.Text);
            }
            userProfileInfo.Education = Convert.ToInt16(this.DDL_Education.SelectedValue);
            try
            {
                if (string.Compare("999", this.DDL_province.SelectedValue) != 0)
                {
                    userProfileInfo.ProvinceId = Convert.ToInt32(this.DDL_province.SelectedValue);
                }
                else
                {
                    userProfileInfo.ProvinceId = -1;
                }
                if (string.Compare("99999", this.DDL_city.SelectedValue) != 0)
                {
                    userProfileInfo.CityId = Convert.ToInt32(this.DDL_city.SelectedValue);
                }
                else
                {
                    userProfileInfo.CityId = -1;
                }
            }
            catch (Exception ex)
            {
            }
            userProfileInfo.VillageId = -1;
            userProfileInfo.Adress = this.txt_address.Text;
            userProfileInfo.PersonalInCome = Convert.ToInt16(this.DDL_PersonalIncome.SelectedValue);
            userProfileInfo.FamilyInCome = Convert.ToInt16(this.DDL_FamilyIncome.SelectedValue);
            userProfileInfo.Credit = "";
            userProfileInfo.Dedication = "";
            userProfileInfo.IDCardValidate = 1;
            userProfileInfo.RealName = this.txt_reanName.Text;
            userProfileInfo.LastUpdate = DateTime.Now;
        }
        catch (Exception ex)
        {
        }
    }

}